<?php
/**
 * Created by PhpStorm.
 * User: allen
 * Date: 2019/3/8
 * Time: 12:48
 */

namespace app\index\model;

use think\Db;
use think\Model;

class Comments extends Model
{
    protected $pk='id';
    protected $autoWriteTimestamp = true;

    /**
     * 留言
     * @param $where
     * @param string $field
     * @param string $order
     * @param int $pageSize
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($where,$field,$order,$pageSize=10){
        //think_comments.*,think_member.nickname as nickname
        $field = $field?$field:"{$this->getTable()}.*,think_member.nickname as nickname,
        think_comment_reply.content as reply_content,think_comment_reply.create_time as reply_time,
        think_comment_reply.love as replay_love,think_comment_reply.mid as reply_mid";
        $list = $this
            ->field($field)
            ->leftJoin('think_member',"think_member.id = {$this->getTable()}.mid")
            ->leftJoin('think_comment_reply',"think_comment_reply.cid = {$this->getTable()}.id")
            ->where($where)
            ->order($order)
            ->paginate($pageSize);
        return $list;
    }

    /**
     * 获取留言
     * @param $where
     * @param $order
     * @param int $pageSize
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList1($where,$order,$pageSize=10){
        $list = $this
            ->withJoin([
                'member'=>['id','nickname','avatar']
            ],'left')
//            ->withJoin('CommentReply','left')
            ->where($where)
            ->order($order)
            ->paginate($pageSize);
        $re = new CommentReply();
        foreach ($list as $k => $v){
            $list[$k]['reply'] = $re->field('id,content,love,create_time')
                ->where('cid','eq',$v['id'])
                ->find();
        }
        return $list;
    }

    public function member(){
        return $this->hasOne('member','id','mid');
    }

    public function CommentReply(){
        return $this->hasOne('CommentReply','cid','id');
    }

    /**
     * @param $cid
     * @param $mid
     * @param string $out
     * @return bool
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function love($cid,$mid,&$out='')
    {
        $info = $this::get($cid);
        if(!$info){
            $out = '评论不存在';
            return false;
        }
        $log = new CommentLog();
        Db::startTrans();
        if($log->check($cid,$mid,$id)){
            $res1 = $log->where('id','eq',$id)->delete();
            $info->love = $info['love']-1;
            $res2 = $info->save();
            if($res2 && $res1){
                $out = '取消点赞';
                Db::commit();
                return true;
            }else{
                $out = '取消点赞失败';
                Db::rollback();
                return false;
            }
        }else{
            $data =[
                'mid'=>$mid,
                'cid'=>$info['id'],
                'create_time'=>time()
            ];
            $res1 = $log->insert($data);
            $info->love = $info['love']+1;
            $res = $info->save();
            if($res && $res1){
                $out = '点赞成功';
                Db::commit();
                return true;
            }else{
                $out = '点赞失败';
                Db::rollback();
                return false;
            }
        }
    }

    /**
     * 回复
     * @param int $id
     * @param int $mid
     * @param string $content
     * @param string $out
     * @return bool
     */
    public function reply($id=0,$mid=0,$content='',&$out=''){
        $info = $this::get($id);
        if(!$info){
            $out='评论不存在';
            return false;
        }
        if($info['is_repay']==1){
            $out = '已经回复了';
            return false;
        }
        $data['cid'] = $id;
        $data['mid'] = $mid;
        $data['content'] = $content;
        $data['create_time'] = time();
        $va = new \app\index\validate\CommentReply();
        if(!$va->check($data)){
            $out = $va->getError();
            return false;
        }
        $info->is_repay = 1;
        Db::startTrans();
        $res = $info->save();
        $repay = new CommentReply();
        $res1 = $repay->insert($data);
        if($res || $res1){
            Db::commit();
            return true;
        }else{
            Db::rollback();
            return false;
        }
    }

    /**
     * @param $value
     * @return false|string
     */
    public function getReplyTimeAttr($value){
        return $value?date('Y-m-d H:i:s',$value):'';
    }
}